package cn.db101.kaoqinserver.service;

import cn.db101.kaoqinserver.domain.Check;
import cn.db101.kaoqinserver.domain.Course;
import cn.db101.kaoqinserver.enumration.CheckStatus;
import cn.db101.kaoqinserver.enumration.CheckType;
import cn.db101.kaoqinserver.mapper.CheckMapper;
import cn.db101.kaoqinserver.model.request.CheckParam;
import cn.db101.kaoqinserver.model.request.CourseParam;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author Admin
* @description 针对表【t_check(签到请假表)】的数据库操作Service
* @createDate 2025-05-07 20:58:49
*/
@Service
public class CheckService {

    @Autowired
    private CheckMapper checkMapper;

    public void save(Check check) {

        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());
        check.setUserId(loginId);
        check.setCheckStatus(CheckStatus.LEAVE.getCode());
        check.setWeekDay(check.getCheckDate().getDayOfWeek().getValue());
        check.setCreateTime(LocalDateTime.now());
        check.setUpdateTime(LocalDateTime.now());
        checkMapper.insert(check);
    }

    public List<Check> leaveList() {

        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());
        return checkMapper.selectList(new LambdaQueryWrapper<Check>()
                .eq(Check::getUserId, loginId)
                .eq(Check::getCheckStatus, CheckStatus.LEAVE.getCode())
                .orderByDesc(Check::getCheckDate)
        );

    }

    public void thirdSave(Check check) {

        check.setCheckDate(LocalDate.now());
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());
        check.setUserId(loginId);
        check.setCheckStatus(CheckStatus.NORMAL.getCode());
        check.setWeekDay(check.getCheckDate().getDayOfWeek().getValue());
        check.setCreateTime(LocalDateTime.now());
        check.setUpdateTime(LocalDateTime.now());

        // 查询是否有进行考勤打卡
        Check localCheck = checkMapper.selectOne(Wrappers.<Check>lambdaQuery()
                .eq(Check::getCourseId, check.getCourseId())
                .apply("DATE(check_date) = {0}", check.getCheckDate())
                .eq(Check::getUserId, loginId)
        );

        if (localCheck == null) {
            checkMapper.insert(check);
        }
    }

    public Page<Check> page(CheckParam checkParam) {

        return checkMapper.selectPage(checkParam, new LambdaQueryWrapper<Check>());
    }

    public Page<Check> LeavePage(CheckParam checkParam) {

        return checkMapper.selectPage(checkParam, new LambdaQueryWrapper<Check>()
                .eq(Check::getCheckStatus, CheckStatus.LEAVE.getCode()));
    }

    public void removeById(Check check) {

        checkMapper.deleteById(check);
    }

    public void changeStatus(Check check) {

        // 先判断是否存在
        check.setWeekDay(check.getCheckDate().getDayOfWeek().getValue());
        Check localCheck = checkMapper.selectOne(Wrappers.<Check>lambdaQuery()
                .eq(Check::getUserId, check.getUserId())
                .eq(Check::getCheckDate, check.getCheckDate())
                .eq(Check::getCourseId, check.getCourseId())
        );

        if (localCheck == null) {
            checkMapper.insert(check);
        } else {
            checkMapper.update(null, Wrappers.<Check>lambdaUpdate()
                    .eq(Check::getId, localCheck.getId())
                    .set(Check::getCheckStatus, check.getCheckStatus())
                    .set(Check::getUpdateTime, LocalDateTime.now())
            );
        }
    }

    public void changeAuditStatus(Check check) {

        checkMapper.update(null, Wrappers.<Check>lambdaUpdate()
                .eq(Check::getId, check.getId())
                .set(Check::getAuditStatus, check.getAuditStatus())
                .set(Check::getUpdateTime, LocalDateTime.now())
        );
    }
}
